"""
获取远程服务器的配置信息
"""
import paramiko
import sys

class SercerInfo():
    def __init__(self,ip,name,password,port=22):
        self.ip = ip
        self.name = name
        self.password = password
        self.port = port

    # 创建ssh连接，__私有
    def __ssh_conn(self):
        ssh_client = paramiko.SSHClient()
        ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
        try:
            ssh_client.connect(hostname=self.ip,username=self.name,password=self.password,port=self.port)
        except Exception as e:
            print("ssh连接失败")
            print(e)
            sys.exit()
        return ssh_client

    # 获取系统版本
    def os_version(self):
        ssh_client = self.__ssh_conn()
        # 查看系统版本文件通用/etc/os-release，centos，redhat的/etc/redhat-release
        stdin,stdout,stderr = ssh_client.exec_command("cat /etc/redhat-release")
        os_version = stdout.read().decode(encoding="UTF-8")
        ssh_client.close()

        return os_version

    # 获取内核版本
    def kernel_version(self):
        ssh_client = self.__ssh_conn()
        # 查看内核版本
        stdin,stdout,stderr = ssh_client.exec_command("uname -r")
        kernel_version = stdout.read().decode(encoding="UTF-8")
        ssh_client.close()

        return kernel_version

    # 获取cpu型号
    def cpu_model(self):
        ssh_client = self.__ssh_conn()
        stdin, stdout, stderr = ssh_client.exec_command("lscpu | grep '型号名称：' | awk -F：'{print $2}'")
        cpu_model = stdout.read().decode(encoding="UTF-8").strip()
        ssh_client.close()

        return cpu_model


if __name__ == '__main__':
    test = SercerInfo(ip="192.168.189.142",name="root",password="123456",port=22)
    print(test.os_version())